from datetime import datetime
from app.db import db

#  物品主表: b_thing
class BThing(db.Model):
    __tablename__ = 'b_thing'

    title = db.Column(db.String(100), nullable=False, comment='物品标题/名称')
    cover = db.Column(db.String(100), comment='封面图链接(如商品主图)')
    description = db.Column(db.Text, comment='详细描述(支持大文本内容)')
    price = db.Column(db.String(50), nullable=False, comment='价格(可存格式化文本,如¥99或协商)')
    status = db.Column(db.String(1), nullable=False, comment='状态(如0-下架､1-上架,需结合业务枚举)')
    repertory = db.Column(db.Integer, nullable=False, comment='库存数量')
    score = db.Column(db.Integer, comment='评分(如0-5分,或关联评论表计算)')
    create_time = db.Column(db.DateTime(6), nullable=False, default=datetime.utcnow, comment='物品创建时间')
    pv = db.Column(db.Integer, default=0, comment='浏览量(Page View)')
    recommend_count = db.Column(db.Integer, default=0, comment='推荐次数/推荐量')
    wish_count = db.Column(db.Integer, default=0, comment='心愿单收藏数')
    collect_count = db.Column(db.Integer, default=0, comment='收藏数')
    classification_id = db.Column(db.BigInteger, db.ForeignKey('b_classification.id'), nullable=False,
                                  comment='关联分类ID(外键,关联b_classification)')
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='物品唯一主键ID')
    # 一对多关系：物品有多个订单
    orders = db.relationship('BOrder', backref='thing', lazy=True)

    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'cover': self.cover,
            'description': self.description,
            'price': self.price,
            'status': self.status,
            'repertory': self.repertory,
            'score': self.score,
            'create_time': self.create_time.isoformat() if self.create_time else None,
            'pv': self.pv,
            'recommend_count': self.recommend_count,
            'wish_count': self.wish_count,
            'collect_count': self.collect_count,
            'classification_id': self.classification_id
        }

    # # 一对多关系：物品属于一个分类
    # # 多对多关系：物品关联多个标签
    # tags = db.relationship('BTag', secondary='b_thing_tag', backref='things', lazy=True)
    # # 一对多关系：物品有多个评论
    # comments = db.relationship('BComment', backref='thing', lazy=True)


